/**
 * table
 * @type {[type]}
 */

import React,{PropTypes,Component,PureComponent}  from 'react';
import { connect } from 'react-redux';
import { Button,Modal,Row,Col,Table,Icon,Pagination,Card} from 'antd';

import columns from './tableColumnsKpi';
import ColumnModal from './ColumnsModal';
import JoinAttention from './JoinAttention';
import { Arrayremoval,JSONToCSVConvertor } from '../../../utils/helper';

import style from './filtertable.less';

const tableColumns = columns.tableColumns;

const FilterTable =({
	dispatch,
	fundpool,
	Homestatus,
	myattention,
	GroupStatus,
	tag,
	isPublic,
	selectedRowKeys
	})=>{
	const {
		columnKpi,
		// selectedRowKeys,//数据筛选
		tableData,//表格数据
		pageSize,
		current,
		GroupList
	} = fundpool;
	const {
		groupNumber,
		username,
		filterProduct,
		MAcolumnKpi,
		// selectedRowKeys,
		MpageSize,
		Mcurrent
	} = myattention;
	console.log("filterTable>>fundpool",fundpool,myattention);
	const fundColumns = [];
	const attenColumns =[];
	if(Homestatus){
		for(var i = 0; i < tableColumns.length; i++){
			var obj = tableColumns[i];
			var num = obj.key;
			var isExist = true;
			for(var j = 0; j < columnKpi.length; j++){
					var aj = columnKpi[j];
					if(aj == num){
						fundColumns.push(obj);
					}
				}
			if(!isExist){
				break;
			}
		}
	}
	if(GroupStatus){
		for(var i = 0; i < tableColumns.length; i++){
			var obj = tableColumns[i];
			var num = obj.key;
			var isExist = true;
			for(var j = 0; j < MAcolumnKpi.length; j++){
					var aj = MAcolumnKpi[j];
					if(aj == num){
						attenColumns.push(obj);
					}
				}
			if(!isExist){
				break;
			}
		}
	}
	//删除
	function onDelete(){
		for(let i = 0;i<tableData.length;i++){
			for(let j=0;j<selectedRowKeys.length;j++){
				if(tableData[i].productId == selectedRowKeys[j]){
					tableData.splice(i,1);
					dispatch({
						type:'fundpool/setState',
						payload:{
							tableData,
						}
					})
					console.log("filterTable>>tableData",tableData);
				}
			}
		}
		dispatch({
			type:'fundpool/setState',
			payload:{
				selectedRowKeys:[]
			}
		})
	}
	//取消关注
	function cancelAttention(){
		const Idarr = [];
		selectedRowKeys.map((item)=>{
			Idarr.push(parseInt(item))
		})
		dispatch({
			type:`${tag}/cancelAttention`,
			payload:{
				"groupNumber":groupNumber,
				"productIds":Idarr,
				"username":username
			}
		})
		dispatch({
			type:`${tag}/setState`,
			payload:{
				selectedRowKeys:[]
			}
		})
	}
	//导出
	function exportFilterPdtData(){
		const headTitle = {};
		const exportArr = [];
		const header =[];
		if(Homestatus){
			for(let i = 0;i<tableData.length;i++){
				for(let j=0;j<selectedRowKeys.length;j++){
					if(tableData[i].productId == selectedRowKeys[j]){
						exportArr.push(tableData[i])
					}
				}
			}
			for(let i = 0;i<fundColumns.length;i++){
				const  key_name = fundColumns[i].dataIndex;
				headTitle[key_name]=fundColumns[i].title;
			}
			header.push(headTitle)
			dispatch({
				type:`${tag}/setState`,
				payload:{
					selectedRowKeys:[]
				}
			})
		}
		if(GroupStatus){
			for(let i = 0;i<filterProduct.length;i++){
				for(let j=0;j<selectedRowKeys.length;j++){
					if(filterProduct[i].productId == selectedRowKeys[j]){
						exportArr.push(filterProduct[i])
						console.log("filterTable>>exportArr",exportArr);
					}
				}
			}
			for(let i = 0;i<attenColumns.length;i++){
				const  key_name = attenColumns[i].dataIndex;
				headTitle[key_name]=attenColumns[i].title;
			}
			header.push(headTitle)
			dispatch({
				type:`${tag}/setState`,
				payload:{
					selectedRowKeys:[]
				}
			})
		}

		JSONToCSVConvertor(exportArr,header[0] );
	}
	//清空列表数据
	function onClearData(){
		dispatch({
			type:`fundpool/setState`,
			payload:{
				tableData:[],
				selectedRowKeys:[],
				pageSize:10,
				current:1
			}
		})
	}

	//表格数据选择
	function onSelectChange(selectedRowKeys) {
		console.log('222222',selectedRowKeys)
		if(Homestatus){
			dispatch({
				type:`fundpool/setState`,
				payload:{
					selectedRowKeys
				}
			})
		}
		if(GroupStatus){
			dispatch({
				type:`myattention/setState`,
				payload:{
					selectedRowKeys:selectedRowKeys
				}
			})
		}
	}
	function onPageSizeChange(page,pageSize){
		dispatch({
			type:`${tag}/setState`,
			payload:{
				pageSize:pageSize,
				current:page
			}
		})
		if(tag == 'myattention'){
			dispatch({
				type:`myattention/setState`,
				payload:{
					MpageSize:pageSize,
					Mcurrent:page
				}
			})
		}
	}
	function onShowSizeChange(current, size){
		dispatch({
			type:`${tag}/setState`,
			payload:{
				pageSize:size,
				current:1
			}
		})
		if(tag == 'myattention'){
			dispatch({
				type:`myattention/setState`,
				payload:{
					MpageSize:size,
					Mcurrent:1
				}
			})
		}
	}
	const rowSelection = {
		selectedRowKeys,
		onChange:onSelectChange,
	};
	const MArowSelection = {
		selectedRowKeys,
		onChange:onSelectChange,
	};
	const FDhasSelected = selectedRowKeys.length > 0;
	const MAhasSelected = selectedRowKeys.length > 0;
	if(Homestatus){
			return (
				<Card>
					<div style={{ marginBottom: 12,marginTop:8 }}>
						<Row type="flex">
							<Col span={2}>
								<JoinAttention
									GroupList={GroupList}
									selectedRowKeys={selectedRowKeys}
									groupNumber={groupNumber}
									hasSelected={FDhasSelected}
									isPublic={isPublic}
								/>
							</Col>
							<Col span={2}>
								<Button
									type="primary"
									disabled={!FDhasSelected}
									onClick={exportFilterPdtData}
								>
									导出
								</Button>
							</Col>
							<Col span={2}>
								<Button
									type="primary"
									onClick={onDelete}
									disabled={!FDhasSelected}
								>
									删除
								</Button>
							</Col>
							<Col span={2}>
								<Button
									type="primary"
									onClick={onClearData}
								>
									清空数据
								</Button>
							</Col>
							<Col span={2} push={14}>
								<ColumnModal
									tableData={tableData}
									Homestatus={Homestatus}
								/>
							</Col>
						</Row>
					</div>
					<Table
						rowSelection={rowSelection}
						rowKey={record => `${record.productId}`}
						columns={fundColumns}
						dataSource={tableData}
						className={style.dataTable}
						pagination={{
							onChange:onPageSizeChange,
							onShowSizeChange:onShowSizeChange,
							showSizeChanger:true,
							defaultCurrent:1,
							defaultPageSize:10,
							total:tableData.length,
							current:current,
							pageSize:pageSize,
							pageSizeOptions:['10','20','30','40','50'],
							showTotal:total => `共 ${total} 条记录`
						}}
					 />
				</Card>
		);
	}
	if(GroupStatus){
		return (
				<Card>
					<div style={{ marginBottom: 12,marginTop:8 }}>
						<Row type="flex" >
							<Col span={4}>
								<Button
									type="primary"
									onClick={cancelAttention}
									disabled={!MAhasSelected}
								>
									取消关注
								</Button>
							</Col>
							<Col span={4}>
								<Button
									type="primary"
									disabled={!MAhasSelected}
									onClick={exportFilterPdtData}
								>
									导出
								</Button>
							</Col>
							<Col span={4} push={13}>
								<ColumnModal
									filterProduct={filterProduct}
									GroupStatus={GroupStatus}
								/>
							</Col>
						</Row>

					</div>
						<Table
							rowSelection={MArowSelection}
							rowKey={record => `${record.productId}`}
							columns={attenColumns}
							dataSource={filterProduct}
							pagination={{
								onChange:onPageSizeChange,
								onShowSizeChange:onShowSizeChange,
								showSizeChanger:true,
								defaultCurrent:1,
								defaultPageSize:10,
								total:tableData.length,
								current:Mcurrent,
								pageSize:MpageSize,
								pageSizeOptions:['10','20','30','40','50'],
								showTotal:total => `共 ${total} 条记录`
							}}
						/>
				</Card>
			);
	}

}

FilterTable.propTypes = {}

export default connect(({fundpool,myattention})=>({fundpool,myattention}))(FilterTable)
